const service = require('../../utils/service.js')
const api = require('../../utils/util.js')
Page({
  data: {
    refun_id: '',
    refunInfo: {},
    orderInfo: {},
    order_id: ''
  },
  onLoad: function (options) {
    let shopInfo = wx.getStorageSync('shopInfo')
    let userInfo = wx.getStorageSync('userInfo')
    let refun_id = options.refun_id
    let order_id = options.order_id
    this.setData({
      refun_id: refun_id,
      order_id: order_id,
      userInfo: userInfo,
      shopInfo: shopInfo
    })
    this.init()
  },

  init: function () {
    // 获取退款详情
    let that = this
    api.ajax(service.url('orderDetail'), {
      unionid: that.data.userInfo.shop_unionid,
      order_id: that.data.order_id
    },
    function (res) {
      if (res.code == 0) {
        that.setData({
          orderInfo: res.data
        })
      } else {
        api.toast(res.message)
      }
    })
  },

  // 前往商品详情
  toGoods: function (e) {
    let goods_id = e.currentTarget.dataset.goodsid
    wx.navigateTo({
      url: `../goods_detail/goods_detail?goods_id=${goods_id}`,
    })
  },

  // 撤销申请
  cancelRefun: function () {
    let that = this
    let refunid = this.data.orderInfo.refund.order_return_id
    wx.showModal({
      title: '提示',
      content: '确定取消申请?',
      success: function (rel) {
        if (rel.confirm) {
          api.ajax(service.url('undoRefund'), {
            unionid: that.data.userInfo.shop_unionid,
            order_return_id: refunid
          },
            function (res) {
              console.log(res)
              if (res.code == 0) {
                api.toast('成功撤销退货、退款申请')
                getCurrentPages()[getCurrentPages().length - 2].init()
                setTimeout(function () {
                  wx.navigateBack({
                    delta: -1
                  });
                }, 800)
              } else {
                api.toast(res.message)
              }
            })
        }
      }
    })
  },

  // 修改申请
  changeRefun: function () {
    let order_id = this.data.orderInfo.id
    wx.navigateTo({
      url: `../refun_apply/refun_apply?order_id=${order_id}`,
    })
  },

  // 引导下载app
  downloadApp: function () {
    wx.showModal({
      title: '提示',
      content: '下载APP更方便联系客服，是否下载?',
      confirmText: '下载',
      success: (res) => {
        if (res.confirm) {
          wx.navigateTo({
            url: '../download_app/download_app',
          })
        }
      }
    })
  }
})